package com.ncu.cloundeoffice.controller;

import com.ncu.cloundeoffice.pojo.ResponseResult;
import com.ncu.cloundeoffice.pojo.TPosition;
import com.ncu.cloundeoffice.service.TPositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/system/basic/pos")
public class TPositionController {


    @Autowired
    private TPositionService tPositionService;

    /**
     * 查询所有的职位
     * @return
     */
    @GetMapping("/")
    public List<TPosition> getAllTPosition(){
        return tPositionService.getAllTPosition();
    }

    /**
     * 添加职位
     * @param tPosition
     * @return
     */
    @PostMapping("/")
    public ResponseResult addTPosition(@RequestBody TPosition tPosition){
        tPosition.setCreateDate(new Date());
        int count = tPositionService.addTPosition(tPosition);
        if(count == 1){
            return ResponseResult.success("添加成功");
        }else {
            return ResponseResult.error("添加失败");
        }
    }

    /**
     * 修改职位
     * @param tPosition
     * @return
     */
    @PutMapping("/")
    public ResponseResult updateTPosition(@RequestBody TPosition tPosition){
        int count = tPositionService.updateTPosition(tPosition);
        if(count == 1){
            return ResponseResult.success("修改成功");
        }else {
            return ResponseResult.error("修改失败");
        }
    }


    /**
     * 删除职位
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseResult deleteTPosition(@PathVariable("id") int id){
       int count = tPositionService.deleteTPosition(id);
        if(count == 1){
            return ResponseResult.success("删除成功");
        }else {
            return ResponseResult.error("删除失败");
        }
    }

     /* *
     * 批量删除职位
     * @param ids
     * @return
     */
    @DeleteMapping("/")
    public ResponseResult deleteTPositionByIds(@RequestParam("ids") int[] ids){
        int count = tPositionService.deleteTPositionByIds(ids);
        if(count > 0){
            return ResponseResult.success("删除成功");
        }else {
            return ResponseResult.error("删除失败");
        }
    }

}
